package proyecto.BusinessLogic;

import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import proyecto.DataAccessObject.VinculacionDAORemote;
import proyecto.EntityBeans.Vinculacion;

@Stateless
@Remote(VinculacionBLRemote.class)
public class VinculacionBL implements VinculacionBLRemote {

	VinculacionDAORemote vinculacionDAORemote;
	
	public int save(Vinculacion vinculacion) {	
		
		try
		{
			Context context = new InitialContext();
			vinculacionDAORemote = (VinculacionDAORemote) context.lookup("VinculacionDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
			
		return vinculacionDAORemote.save(vinculacion); //alta de vinculacion		
		
	}
		
	public Vinculacion findById(int id) {
		try
		{
			Context context = new InitialContext();
			vinculacionDAORemote = (VinculacionDAORemote) context.lookup("VinculacionDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
			
		return vinculacionDAORemote.findById(id);
		
	}
	
		
	public void merge(Vinculacion vinculacion) {
		try
		{
			Context context = new InitialContext();
			vinculacionDAORemote = (VinculacionDAORemote) context.lookup("VinculacionDAO/remote");
			vinculacionDAORemote.merge(vinculacion);
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}	
		
	}
	
	public List findByName(String name)
	{	
		try
		{
			Context context = new InitialContext();
			vinculacionDAORemote = (VinculacionDAORemote) context.lookup("VinculacionDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
	    return vinculacionDAORemote.findByName(name);
	}
	
	public List findAll() {		
		try
		{
			Context context = new InitialContext();
			vinculacionDAORemote = (VinculacionDAORemote) context.lookup("VinculacionDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		return vinculacionDAORemote.findAll();
	}
	
	public List findByIdList(int id)
	{	     
		try
		{
			Context context = new InitialContext();
			vinculacionDAORemote = (VinculacionDAORemote) context.lookup("VinculacionDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
	    return vinculacionDAORemote.findByIdList(id);
	}
	
		
	public void remove(int idVinculacion){	
		try
		{
			Context context = new InitialContext();
			vinculacionDAORemote = (VinculacionDAORemote) context.lookup("VinculacionDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		vinculacionDAORemote.remove(idVinculacion);
	}
	
	public void refresh(Vinculacion vinculacion){
		try
		{
			Context context = new InitialContext();
			vinculacionDAORemote = (VinculacionDAORemote) context.lookup("VinculacionDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		vinculacionDAORemote.refresh(vinculacion);
	}
}
